﻿using System;
using System.Windows;
using System.Windows.Threading;

namespace LaunchPad.Core.Dialogs
{
    public partial class StartShowDialog : Window
    {
        private int _countdown;
        private DispatcherTimer _countdownTimer;

        public StartShowDialog()
        {
            InitializeComponent();
        }

        private void StartButtonClick(object sender, RoutedEventArgs e)
        {
            startButton.IsEnabled = false;
            countdownBorder.Visibility = Visibility.Visible;
            _countdownTimer = new DispatcherTimer();
            _countdownTimer.Tick += CountdownTimerTick;
            _countdownTimer.Interval = TimeSpan.FromSeconds(1);
            _countdown = 9;
            _countdownTimer.Start();
        }

        private void CountdownTimerTick(object sender, EventArgs e)
        {
            if (_countdown == 0)
            {
                DialogResult = true;
                _countdownTimer.Stop();
                Close();
            }
            else
            {
                countdownTextBlock.Text = _countdown.ToString();
                _countdown--;
            }
        }

        private void AbortButtonClick(object sender, RoutedEventArgs e)
        {
            if (_countdownTimer != null)
                _countdownTimer.Stop();
            DialogResult = false;
        }
    }
}